﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;
using System.Threading;
using System.Web;
using System.Net;
using System.Management;
using System.Net.Sockets;

namespace comm
{
    public partial class fenge : Form
    {
        public fenge()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (serialPort2.IsOpen == false)
            {
                serialPort2.Open();
            }
            strToToHexByte(this.textBox1.Text.Trim());
        }

        private  void strToToHexByte(string hexString)
        {
            hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
                hexString += " ";
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            serialPort2.Write(returnBytes, 0, returnBytes.Length);
        }







    }
}
